package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.MedicalMapper;
import com.itheima.pojo.Medical;
import com.itheima.service.MedicalService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.itheima.constant.MessageConstant.*;

@Service(version = "1.0",interfaceClass = MedicalService.class)
@Transactional
public class MedicalServiceImpl implements MedicalService {

    @Resource
    MedicalMapper medicalMapper;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Page<Medical> medicalList = medicalMapper.findByCondition(queryPageBean.getQueryString());

        PageResult pageResult = new PageResult();
        pageResult.setRows(medicalList.getResult());
        pageResult.setTotal(medicalList.getTotal());
        return Result.success(QUERY_MEDICAL_SUCCESS,pageResult);
    }

    @Override
    public Result save(Medical medical) {
        medicalMapper.save(medical);
        return Result.success(ADD_MEDICAL_SUCCESS,null);
    }

    @Override
    public Result findById(Integer medicalId) {
        Medical medical = medicalMapper.findById(medicalId);
        return Result.success(QUERY_MEDICAL_SUCCESS,medical);
    }

    @Override
    public Result update(Medical medical) {
        medicalMapper.update(medical);
        return Result.success(UPDATE_MEDICAL_SUCCESS,null);
    }

    @Override
    public Result delete(Integer medicalId) {
        medicalMapper.delete(medicalId);
        return Result.success(DELETE_MEDICAL_SUCCESS,null);
    }
}
